package com.ljkj.cordial.chat.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.bean.EmojiPackage;
import com.nic.view.adapter.CommonAdapter;
import com.nic.view.util.JLog;
import com.nic.view.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

public class EmojiAdapter extends CommonAdapter<String> {
    private String[] emojiStr = {"\uD83D\uDE00", "\uD83D\uDE2C", "\uD83D\uDE01"
            , "\uD83D\uDE02", "\uD83D\uDE03", "\uD83D\uDE04", "\uD83E\uDD23", "\uD83D\uDE05"
            , "\uD83D\uDE06", "\uD83D\uDE07", "\uD83D\uDE09", "\uD83D\uDE0A", "\uD83D\uDE42", "\uD83D\uDE43",
            "\uD83D\uDE0B", "\uD83D\uDE0C", "\uD83D\uDE0D", "\uD83D\uDE17", "\uD83D\uDE1A", "\uD83E\uDD2A", "\uD83D\uDE1C", "\uD83D\uDE1D",
            "\uD83D\uDE0F", "\uD83D\uDE36", "\uD83D\uDE12", "\uD83D\uDE44", "\uD83D\uDE1E", "\uD83D\uDE24", "\uD83D\uDE35", "\uD83D\uDE32", "\uD83E\uDD2C"};
    private Context context;

    public EmojiAdapter(Context context) {
        this.context = context;
        List<String> list = new ArrayList<>();
        String[] emojis = EmojiPackage.getEmojTags(context);
        if (emojis.length > 0) {
            for (String emoji : emojis) {
                list.add(emoji);
            }
        }
        setList(list);
    }

    public EmojiAdapter() {
        List<String> list = new ArrayList<>();
        for (String s : emojiStr) {
            list.add(s);
        }
        setList(list);
    }

    @Override
    public View getBindView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = new LinearLayout(parent.getContext());
//           int  dp240= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,240,parent.getResources().getDisplayMetrics());
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT
                    , ViewGroup.LayoutParams.WRAP_CONTENT);
            convertView.setLayoutParams(params);
            int dp16 = parent.getResources().getDimensionPixelOffset(R.dimen.dimen_32);
            int dp8 = parent.getResources().getDimensionPixelOffset(R.dimen.dimen_4);
            ImageView imageView = new ImageView(parent.getContext());
            imageView.setBackgroundResource(R.color.title_color);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                    dp16
                    , dp16);
            imageView.setLayoutParams(params1);
            imageView.setPadding(dp8, dp8, dp8, dp8);
            ((LinearLayout) convertView).addView(imageView);
            holder = new ViewHolder();
            holder.tvEmoji = imageView;
            holder.tvEmoji.setScaleType(ImageView.ScaleType.FIT_XY);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String tag = (String) getItem(position);
        if (!tag.equals(holder.tvEmoji.getTag())) {
            holder.tvEmoji.setTag(null);
            String filePath = EmojiPackage.getAbsoluteFileNameByEmojiPositionTag(tag);
            GlideUtil.getInstance().load(filePath, holder.tvEmoji);
            holder.tvEmoji.setTag(tag);
        }

        return convertView;
    }

    class ViewHolder {
        ImageView tvEmoji;
    }
}
